#! /usr/bin/env python
import opentick
import logging
from   sys       import exit
from   time      import sleep
from   getpass   import getpass

class MyClient( opentick.OTClient ):
    """A simple OTClient subclass that illustrate the use of the Python module.
    """
    #
    def onLogin( self ):
        """Display a message when login is successful.
        """
        logging.info( "Login successful." )
        return 
    #   
    def onError( self, otError ):
        """Display a message on error and exit the script immediately.
        """
        logging.error( otError.getDescription() )
        exit()
#
# Run the remaining lines if we are in the top-level script environment.
#
if __name__ == "__main__":
    logging.basicConfig( level  = logging.DEBUG,
                         format = '%(asctime)s %(levelname)s %(message)s' )
    #
    # Create an instance of our OpenTick client and add a server host.
    #
    client = MyClient()
    client.addHost( "feed2.opentick.com", 10010 )
    #
    # Ask the user for a login account name and password to connect to the 
    # OpenTick server.
    #
    username = raw_input( "Enter the user name: " )
    password = getpass( "Password for " + username + ": " )
    #
    # Initiate login process. 
    #
    client.login( username, password )
    #
    # Wait for the login to succeed. 
    #
    while not client.isLoggedIn():
        logging.info( "Waiting for login to complete." )
        sleep(1)

